package com.jdbc.DAO;
import com.jdbc.Conn;
import com.model.CourseInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by NULL on 2017/12/14.
 */
public class CourseInfoServer {

    private Connection conn = Conn.getConnection();
    private PreparedStatement ps;
    public boolean addCourse(CourseInfo courseInfo){
        final String sql= "insert into courseinfo values(?,?,?,?)";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, courseInfo.getCourse_id());
            ps.setString(2, courseInfo.getCourse_name());
            ps.setInt(3, courseInfo.getCourse_credit());
            ps.setString(4, courseInfo.getCourse_teacher());
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }


    public List<CourseInfo> queryAllCourse(){
        List<CourseInfo> allCourse = new ArrayList<CourseInfo>();
        final String sql = "select * from courseInfo";
        try {
            ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                CourseInfo courseInfo = new CourseInfo();
                courseInfo.setCourse_id(rs.getInt("course_id"));
                courseInfo.setCourse_name(rs.getString("course_name"));
                courseInfo.setCourse_credit(rs.getInt("course_credit"));
                courseInfo.setCourse_teacher(rs.getString("course_teacher"));
                allCourse.add(courseInfo);
            }
            return allCourse;

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public CourseInfo queryCourseById(int course_id){

        CourseInfo courseInfo=new CourseInfo();
        final String sql = "select * from courseInfo where course_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,course_id);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                courseInfo.setCourse_id(rs.getInt("course_id"));
                courseInfo.setCourse_name(rs.getString("course_name"));
                courseInfo.setCourse_credit(rs.getInt("course_credit"));
                courseInfo.setCourse_teacher(rs.getString("course_teacher"));
                return courseInfo;
            }
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }


    public boolean updateCourse(CourseInfo courseInfo){
        final String sql = "update courseInfo set course_id=?,course_name=?,course_credit=?,course_teacher=? where course_id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, courseInfo.getCourse_id());
            ps.setString(2, courseInfo.getCourse_name());
            ps.setInt(3, courseInfo.getCourse_credit());
            ps.setString(4, courseInfo.getCourse_teacher());
            ps.setInt(5, courseInfo.getCourse_id());
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean deleteCourse(int course_id){
        final String sql = "delete from courseinfo where course_id = ?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, course_id);
            ps.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }
}
